package com.wenyu.wenyuyuedu.activity.circle;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.wenyu.wenyuyuedu.R;
import com.wenyu.wenyuyuedu.activity.LoginActivity;
import com.wenyu.wenyuyuedu.adapter.circleAdapter.EdittextCircleAdater;
import com.wenyu.wenyuyuedu.bean.circle.CircleDataBean;
import com.wenyu.wenyuyuedu.bean.circle.ShowCircleType;
import com.wenyu.wenyuyuedu.global.ApiWrapper;
import com.wenyu.wenyuyuedu.server.circle.CircleDataServer;
import com.wenyu.wenyuyuedu.server.circle.CreateCirlceServer;
import com.wenyu.wenyuyuedu.utils.AbSharedUtil;
import com.wenyu.wenyuyuedu.utils.MyToast;
import com.wenyu.wenyuyuedu.utils.SharedConstants;
import com.wenyu.wenyuyuedu.utils.StatusBarUtil;
import com.wenyu.wenyuyuedu.view.NoScrollGridView;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static anetwork.channel.http.NetworkSdkSetting.context;

/*查看圈子*/
public class ExamineCircleActivity extends AppCompatActivity {
    @InjectView(R.id.iv_left)
    ImageView ivLeft;
    @InjectView(R.id.rl_circle_title)
    RelativeLayout rlCircleTitle;
    @InjectView(R.id.examin_circle_conver)
    ImageView examinCircleConver;
    @InjectView(R.id.examin_circle_name)
    TextView examinCircleName;
    @InjectView(R.id.tv_examin_type)
    TextView tvExaminType;
    @InjectView(R.id.examin_circle_content)
    TextView examinCircleContent;
    @InjectView(R.id.activity_examine_circle)
    LinearLayout activityExamineCircle;
    @InjectView(R.id.create_circle_gv)
    NoScrollGridView createCircleGv;
    private Activity activity;
    private Intent intentGet;
    private String circleId;
    private Intent intent;
    private EdittextCircleAdater adater;
    private List<ShowCircleType.InfoBean> info;
    private String circleType;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_examine_circle);
        ButterKnife.inject(this);
        this.activity = ExamineCircleActivity.this;
        StatusBarUtil.setStatusBar(ExamineCircleActivity.this, R.color.white);
        initView();
        requestData();
        request();
    }
    /*
     * 网络请求
     * */
    private void request() {
        CreateCirlceServer createCirlceServer = ApiWrapper.getInstance().create(CreateCirlceServer.class);
        createCirlceServer.getType().enqueue(new Callback<ShowCircleType>() {
            @Override
            public void onResponse(Call<ShowCircleType> call, Response<ShowCircleType> response) {
                ShowCircleType body = response.body();
                if (body != null) {
                    int code = body.getCode();
                    if (code == 1) {
                        info = body.getInfo();
                        adater.addAll(info);
                        adater.getCircleType(circleType);
                    } else if (code == 4000) {
                        MyToast.showToast(activity, "系统繁忙，请稍后重试", Toast.LENGTH_SHORT);
                    }
                }
            }

            @Override
            public void onFailure(Call<ShowCircleType> call, Throwable t) {
                MyToast.showToast(activity, "系统繁忙，请稍后重试", Toast.LENGTH_SHORT);
            }
        });
    }

    /*展示数据*/
    private void requestData() {
        String token = AbSharedUtil.getString(activity, SharedConstants.USER_TOKEN);
        CircleDataServer circleDataServer = ApiWrapper.getInstance().create(CircleDataServer.class);
        Call<CircleDataBean> circle = circleDataServer.getCircle(token, Integer.parseInt(circleId), 1);
        circle.enqueue(new Callback<CircleDataBean>() {
            @Override
            public void onResponse(Call<CircleDataBean> call, Response<CircleDataBean> response) {
                CircleDataBean body = response.body();
                if (body != null) {
                    int code = body.getCode();
                    if (code == 1) {
                        CircleDataBean.InfoBean info = body.getInfo();
                        if (info != null) {
                            /*圈子的封面*/
                            Glide.with(activity)
                                    .load(Uri.parse(info.getCover()))
                                    .error(R.drawable.login_logo)
                                    .into(examinCircleConver);
                            //圈子的名字
                            examinCircleName.setText(info.getTitle());
                            //圈子的类名
                            circleType = info.getCircleType();
                            tvExaminType.setText(circleType);
                            //圈子的介绍
                            examinCircleContent.setText(info.getContent());
                        }

                    } else if (code == 0) {
                        //token错误或者已失效
                        showLoginDoalog();
                    } else if (code == 3600) {
                        //需要您登陆之后才可以查看的哦!
                        MyToast.showToast(activity, "需要您登陆之后才可以查看的哦！", Toast.LENGTH_SHORT);
                    } else if (code == 3603) {
                        //系统繁忙，请您稍后再试！
                        MyToast.showToast(activity, "系统繁忙，请您稍后再试！", Toast.LENGTH_SHORT);
                    } else if (code == 3605) {
                        //您必须要先加入书圈之后才可以查看哦！
                        MyToast.showToast(activity, "您必须要先加入书圈之后才可以查看哦！", Toast.LENGTH_SHORT);
                    } else if (code == 3604) {
                        //圈子不存在或者被冻结了！
                        MyToast.showToast(activity, "圈子不存在或者被冻结了！", Toast.LENGTH_SHORT);
                    }
                }
            }

            @Override
            public void onFailure(Call<CircleDataBean> call, Throwable t) {
                MyToast.showToast(activity, "网络异常，请稍后重试！", Toast.LENGTH_SHORT);
            }
        });
    }

    /*初始化控件*/
    private void initView() {
        intent = new Intent();
        intentGet = getIntent();
        circleId = intentGet.getStringExtra("helpfeedbackactivity_to_examinactivity");
        ivLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        /*gredview的点击事件---选取类型*/
        adater = new EdittextCircleAdater(activity);
        createCircleGv.setAdapter(adater);
    }

    /*token错误或失效*/
    AlertDialog ad;
    private TextView quxiao;
    private TextView queding;

    private void showLoginDoalog() {

        ad = new AlertDialog.Builder(activity).create();
        ad.show();
        Window window = ad.getWindow();
        window.setContentView(R.layout.dailog_login);
        quxiao = (TextView) window.findViewById(R.id.dialog_login_quxiao);
        queding = (TextView) window.findViewById(R.id.dialog_login_queding);
        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ad.dismiss();
            }
        });
        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AbSharedUtil.putString(context, SharedConstants.USER_TOKEN, "");
                intent.setClass(activity, LoginActivity.class);
                startActivity(intent);
                ad.dismiss();
            }
        });
    }
}
